package hr_project.hr.web.Emp;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import hr_project.hr.biz.Archives.IArchivesBiz;
import hr_project.hr.biz.Emp.IEmpBiz;
import hr_project.hr.biz.Emp.IEmpMoveBiz;
import hr_project.hr.entity.Archives;
import hr_project.hr.entity.Emp;
import hr_project.hr.entity.EmpMove;
import hr_project.hr.entity.Empmove_result;
import hr_project.hr.util.QueryEmpMove;
import hr_project.hr.util.pagebean;

@Controller
@RequestMapping("/empmove")
public class EmpMoveAction {

	@Autowired
	private IEmpMoveBiz biz;
    @Autowired
	private IEmpBiz ebiz;
    @Autowired
    private IArchivesBiz abiz;
    
	
	

	public IArchivesBiz getAbiz() {
		return abiz;
	}

	public void setAbiz(IArchivesBiz abiz) {
		this.abiz = abiz;
	}

	public IEmpBiz getEbiz() {
		return ebiz;
	}

	public void setEbiz(IEmpBiz ebiz) {
		this.ebiz = ebiz;
	}

	public IEmpMoveBiz getBiz() {
		return biz;
	}

	public void setBiz(IEmpMoveBiz biz) {
		this.biz = biz;
	}
	
	//列表
	@RequestMapping("/list")
	@ResponseBody
	public pagebean getAll(@ModelAttribute(value="query")QueryEmpMove query,@RequestParam(defaultValue="1")int page)
	{
		String pb=JSON.toJSONString(biz.getByPage(query, page));
		System.out.println("empmove的列表"+pb);
		return biz.getByPage(query, page);
	}
	
	//增加
	@RequestMapping("/add")
	public String addEmpMove(HttpSession sess,EmpMove emo)
	{
		Emp emp=(Emp)sess.getAttribute("loger");
		emo.setMo_check(emp);
		emo.setOld_de_id(emp.getUs_depid());
		emo.setOld_ro_id(emp.getRo_id());
		if(emo.getMo_type().equals("调职"))
		{
			biz.addEmpMove(emo);
		}else
		{
			biz.addEmpMove_arrive(emo);
		}
		return "my-empmovelist";
	}
	
	//登陆人的调度列表
	@RequestMapping("/mylist")
	@ResponseBody
	public Map getAll(HttpSession sess,@RequestParam(defaultValue="1")int page,@RequestParam(defaultValue="8")int pagesize)
	{
		Emp e=(Emp)sess.getAttribute("loger");
		Map map=new HashMap();
		List<EmpMove> list=biz.getByCreator(e.getUs_id(), page, pagesize);
		map.put("rows",list);
		map.put("total",biz.getCountBylogid(e.getUs_id()) );
		return map;
	}
	
	//提交
	@RequestMapping("/apply")
	public void commit(long id,HttpServletResponse res) throws IOException
	{
		EmpMove emo=biz.getById(id);
		emo.setMo_check_status("待处理");
		if(emo.getOld_ro_id().getRo_id()==2||emo.getOld_ro_id().getRo_id()==4)
		{
			Emp boss=new Emp();
			boss.setUs_id(1);
			emo.setDealman(boss);
		}else
		{
			emo.setDealman(emo.getOld_de_id().getDe_manager());
		}
		biz.modEmpMove(emo);
		res.getWriter().print("succ");
	}
	
	//删除
	@RequestMapping("/del/{id}")
	public String del(@PathVariable(value="id")long id)
	{
		biz.delEmpMove(id);
		return "my-empmovelist";
	}
	
	//获取一个
	@RequestMapping("/getbyid/{n}")
	public ModelAndView getByid(long id,@PathVariable("n")int n)
	{
		EmpMove emo=biz.getById(id);
		ModelAndView mv=new ModelAndView();
		if(n==1)
		{
		     mv.setViewName("empmove-mod");
			
		}else
		{
			mv.setViewName("empmove-det");
		}
		
		mv.addObject("emo", emo);
		return mv;
	}
	
	//修改
	@RequestMapping("/mod")
	public String mod(@ModelAttribute(value="mov")EmpMove mov)
	{
		biz.modEmpMove(mov);
		return "my-empmovelist";
	}
	
	//根据处理人找到一组调度表
	@RequestMapping("/apply-list")
	@ResponseBody
	public List<EmpMove> getAllBydealer(HttpSession sess)
	{
		Emp e=(Emp)sess.getAttribute("loger");
		List<EmpMove> list=biz.getByCheck(e.getUs_id());
		return list;
	}
	
	//原部门经理进行审核通过后，交给新部门经理审核
	@RequestMapping("/apply-newdept/{id}")
	public String apply2(HttpSession sess,@PathVariable("id")long id,Empmove_result res)
	{
		Emp e=(Emp)sess.getAttribute("loger");
		res.setCheck_emp(e.getUs_id());
		biz.addRes(res);
		EmpMove emo=biz.getById(id);
		
		/***判断调度表的类型是离职还是调职，如果是调职则需要进行一系列的审核
		 * 如果是离职，只需要原部门经理进行审核，原部门经理审核通过后交给人事
		*/
		//调职
		if(emo.getMo_type().equals("调职"))
		{
			long newdept=emo.getNew_de_id().getDe_manager().getUs_id();
			if(res.getRes_result().equals("通过"))
			{
				//如果审核结果是通过就进行以下操作
				//如果处理人是总经理或者新部门的经理，就把处理人改成人事经理
				if(emo.getDealman().getUs_id()==1||emo.getDealman().getUs_id()==newdept)
				{
					emo.setMo_check_status("待处理");
					Emp hrman=new Emp();
					hrman.setUs_id(5);
					emo.setDealman(hrman);
				}else if(emo.getDealman().getUs_id()==emo.getOld_de_id().getDe_manager().getUs_id())
				{
					//如果处理人是原部门经理就把处理人改成申请人申请的部门经理
					emo.setMo_check_status("待处理");
					emo.setDealman(emo.getNew_de_id().getDe_manager());
				}else if(emo.getDealman().getUs_id()==5)
				{
					//如果人事通过了,就把员工的部门修改成申请的新部门
					emo.setMo_check_status("通过");
					Emp emp=ebiz.getById(emo.getMo_check().getUs_id());
					emp.setUs_depid(emo.getNew_de_id());
					ebiz.modEmp(emp);
				}
			}else
			{
				//如果结果不是通过，就把员工调度表的状态改为审核结果,处理人是登陆人
				emo.setMo_check_status(res.getRes_result());
				emo.setDealman(e);
			}
		}else
		{
			//离职
			if(res.getRes_result().equals("通过"))
			{
				//如果处理部门经理或者总经理,就把处理人改成人事经理
				if(emo.getDealman().getUs_id()==1||emo.getDealman().getUs_id()==emo.getOld_de_id().getDe_manager().getUs_id())
				{
					emo.setMo_check_status("待处理");
					Emp hrman=new Emp();
					hrman.setUs_id(4);
					emo.setDealman(hrman);
				}else if(emo.getDealman().getUs_id()==4)
				{
					//如果处理人是人事经理，就把档案状态改为已删除
					emo.setMo_check_status("通过");
					Emp emp=ebiz.getById(emo.getMo_check().getUs_id());
					Archives ar=abiz.getByid(emp.getA_id().getAr_id());
					ar.setAr_status("已删除");
					abiz.modArchives(ar);
				}
				
			}else
			{
				//如果结果不是通过，就把员工调度表的状态改为审核结果,处理人是登陆人
				emo.setMo_check_status(res.getRes_result());
				emo.setDealman(e);
			}
		}
		biz.modEmpMove(emo);
		return "empmove-applylist";
	}

}
